/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package core.game;

import core.io.Message;
import core.io.Session;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;

/**
 *
 * @author Dung Pham
 */
public class BoardGame implements Runnable, Comparable<Object> {

    private int boardID;
    private AtomicBoolean playing = new AtomicBoolean(false);
    private String password = "";
    private AtomicInteger money = new AtomicInteger(0);
    private long currentMoveTime;
    private AtomicInteger minPlayer, maxPlayer;
    private static final Object LOCK = new Object();
    private BlockingQueue<Message> messages;
    private final Object initializeSessionLock = new Object();
    private int maxPlayerInGame, minPlayerInGame;
    private boolean initialization = false;

    public BoardGame(int boardID, int minPlayer, int maxPlayer) {
        this.minPlayer = new AtomicInteger(minPlayer);
        this.maxPlayer = new AtomicInteger(maxPlayer);
        this.minPlayerInGame = minPlayer;
        this.maxPlayerInGame = maxPlayer;
        messages = new LinkedBlockingQueue<>();
    }

    protected final void initialize() {
//        Reporter.infor("initialize " + getServiceId() + " R" + getRoom().getRoomId() + " B" + getControllerId());
        if (!initialization) {
            synchronized (initializeSessionLock) {
                if (!initialization) {
                    //setIdleTime();
                    initialization = true;
                    //isStop = false;
                    GameThreadPool.getInstance().runTask(this);
                }
            }
        }
    }

    @Override
    public void run() {
        while (true) {
            Message m = null;
            try {
//                m = messages.poll(1, TimeUnit.SECONDS);
//                if (m != null) {
//                    if (m.getSession() == null) {
//                        continue;
//                    }
//
//                    Session session = findSession(m.getSession());
//                    if (session == null) {
//                        continue;
//                    }
//
//                    try {
//                        long processingTime = System.currentTimeMillis();
//                        processMessage(session, m);
//                        processingTime = System.currentTimeMillis() - processingTime;
//                        if (processingTime > Definition.SLOW_PROCESS_MSG_TIME) {
//                            Reporter.logSlowedProcessCommand(m.command, processingTime, player.getUsername(), player.toString());
//                        }
//                    } catch (Exception e) {
//                        Reporter.error("Process message got exception : r" + getRoom().getRoomId() + " b" + getControllerId(), e);
//                    }
//                }
//
//                if (isStop) {
//                    break;
//                }
//
//                update();
//                User owner = getOwner();
//
//                if (messages.isEmpty() && owner != null) {
//                    /*
//                     * If chu phong dang co mat nhung ko lam gi het, sau 1 phut
//                     * kick ra
//                     */
//                    if (getServiceId() == Service.PHOM || getServiceId() == Service.TIENLEN
//                            || getServiceId() == Service.NEWPHOM || getServiceId() == Service.TIENLENMIENBAC || getServiceId() == Service.POKER) {
//                        // <editor-fold defaultstate="collapsed" desc="Xu ly cho cac game Tien Len & Phom">
//                        if ((System.currentTimeMillis() - idleTime) > Definition.IDLE_TIME_OWNER && !isPlaying()) {
//                            //update();
//                            kickPlayer(owner.getUserId());
//                            setIdleTime();
//                        }
//                        //</editor-fold>
//                    } else {
//                        /*
//                         * Kich nhung thang nao trong phong ma khong lam gi het
//                         * trong 1 phut.
//                         */
//                        // <editor-fold defaultstate="collapsed" desc="Xu ly cho cac game service khac">
//                        for (int i = 0; i < playerSize(); i++) {
//                            User player = getUser(i);
//                            if (player != null) {
//                                long lastActiveTime = (Long) player.getSession().getLastActiveTime();
//                                if ((System.currentTimeMillis() - lastActiveTime) > Definition.IDLE_TIME_OWNER && !isPlaying()
//                                        && (System.currentTimeMillis() - getCurrentMoveTime()) > Definition.IDLE_TIME_OWNER) {
//                                    kickPlayer(player.getUserId());
//                                    owner = getOwner();
//                                    if (owner != null) {
//                                        owner.getSession().setAttribute(AttributeName.LAST_ACTIVE_TIME, System.currentTimeMillis());
//                                    }
//                                    break;
//                                }
//                            }
//                        }
//                        // </editor-fold>
//                    }
//                } else if (owner == null) {
//                    initialization = false;
//                    break;
//                }
            } catch (Exception ex) {
                //Logger.getLogger(IwinLoggerNames.ERROR_DB_LOG_NAME).error("Board running got exception : ", e);
                //Reporter.error("GameController process message got exception :" + getServiceId() + "r" + getRoom().getRoomId() + "b" + getControllerId(), e);
            }
        }
    }

    @Override
    public int compareTo(Object o) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
    
    public void proccessMessage(Session session, Message message){
        
    }
}
